<template>
  <div class="home-container">
    <!-- 导航栏 -->
    <nav class="game-nav">
      <div class="user-profile">
        <img :src="user.avatar" class="user-avatar" alt="玩家头像">
        <span class="user-gold">💰 {{ user.gold }}</span>
        <span class="user-points">🏆 {{ user.points }}</span>
      </div>
      <button class="btn btn-primary" @click="showCreateRoomModal">创建房间</button>
    </nav>

    <!-- 游戏状态区 -->
    <div class="game-status">
      <div class="status-card online-players">
        <h3>在线玩家</h3>
        <p class="status-number">{{ onlineCount }}</p>
      </div>
      <div class="status-card active-games">
        <h3>进行中的游戏</h3>
        <p class="status-number">{{ activeGames }}</p>
      </div>
    </div>

    <!-- 房间列表 -->
    <div class="room-list">
      <div v-for="room in rooms" :key="room.id" class="room-card" @click="joinRoom(room.id)">
        <div class="room-header">
          <span class="room-name">{{ room.name }}</span>
          <span class="room-mode" :class="room.mode">{{ room.modeLabel }}</span>
        </div>
        <div class="room-info">
          <span>👥 {{ room.players }}/4</span>
          <span>🕹 {{ room.status }}</span>
        </div>
        <button class="btn btn-join">立即加入</button>
      </div>
    </div>

    <!-- 快速操作 -->
    <div class="quick-actions">
      <button class="btn btn-quick-join" @click="quickJoin" :disabled="isMatching">
        <span v-if="isMatching">匹配中...</span>
        <span v-else>快速加入</span>
      </button>
      <button class="btn btn-tutorial" @click="showTutorial">游戏教程</button>
    </div>
  </div>
</template>

<script setup>
import { ref } from 'vue';

// 模拟用户数据
const user = ref({
  avatar: 'default-avatar.png',
  gold: 1500,
  points: 3200
});

// 房间列表
const rooms = ref([
  { id: 1, name: '初级竞技场', mode: 'classic', players: 3, status: '等待中' },
  { id: 2, name: '高手对决', mode: 'ranked', players: 2, status: '准备中' },
  { id: 3, name: '好友房间', mode: 'private', players: 4, status: '进行中' }
]);

// 在线人数和活跃游戏数量
const onlineCount = ref(856);
const activeGames = ref(123);

// 匹配状态
const isMatching = ref(false);

// WebSocket 连接
const wsUrl = "ws://localhost:8081/socket.io"; // 替换为你的后端 WebSocket 地址
const socket = new WebSocket(wsUrl);

// 监听 WebSocket 消息
socket.addEventListener("message", (event) => {
  const message = JSON.parse(event.data);
  console.log("Received message from server:", message);

  if (message.type === "MATCH_SUCCESS") {
    isMatching.value = false;
    alert(`匹配成功！房间号: ${message.roomId}`);
  } else if (message.type === "ERROR") {
    alert(`匹配失败: ${message.reason}`);
  }
});

// 生成随机用户名和用户ID
const generateRandomUsername = () => {
  return `${Math.random().toString(36).substr(2, 8)}`;
};

const generateRandomUserId = () => {
  return `${Math.random().toString(36).substr(2, 8)}`;
};

// 快速加入匹配
const quickJoin = () => {
  if (isMatching.value) {
    alert("已经在匹配中...");
    return;
  }

  isMatching.value = true;
  const username = generateRandomUsername();
  const userId = generateRandomUserId();

  const matchRequest = {
    type: "MATCH_REQUEST",
    username: username, // 随机生成的用户名
    userId: userId // 随机生成的用户ID
  };

  // 发送匹配请求到后端
  socket.send(JSON.stringify(matchRequest));
};
</script>

<style lang="scss" scoped>
.home-container {
  max-width: 1200px;
  margin: 0 auto;
  padding: 20px;
  background: var(--game-bg-color, #1a1a1a);
  color: var(--text-primary, #fff);
}

.game-nav {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 2rem;
  padding: 1rem;
  background: rgba(0,0,0,0.3);
  border-radius: 8px;

  .user-profile {
    display: flex;
    align-items: center;
    gap: 1rem;

    .user-avatar {
      width: 40px;
      height: 40px;
      border-radius: 50%;
      border: 2px solid gold;
    }
  }
}

.room-list {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(280px, 1fr));
  gap: 1.5rem;
  margin: 2rem 0;
}

.room-card {
  background: var(--card-bg-color, #2d2d2d);
  padding: 1.5rem;
  border-radius: 12px;
  cursor: pointer;
  transition: transform 0.2s ease;

  &:hover {
    transform: translateY(-5px);
    box-shadow: 0 4px 15px rgba(255,215,0,0.2);
  }

  .room-header {
    display: flex;
    justify-content: space-between;
    margin-bottom: 1rem;

    .room-name {
      font-weight: bold;
      color: var(--accent-color, #ffd700);
    }

    .room-mode {
      padding: 0.2rem 0.5rem;
      border-radius: 4px;
      font-size: 0.8em;

      &.classic { background: #4CAF50 }
      &.ranked { background: #F44336 }
      &.private { background: #2196F3 }
    }
  }
}

.btn {
  padding: 0.8rem 1.5rem;
  border: none;
  border-radius: 6px;
  font-weight: bold;
  transition: all 0.3s ease;

  &-primary {
    background: linear-gradient(135deg, #ffd700, #ff9800);
    color: #1a1a1a;

    &:hover {
      opacity: 0.9;
    }
  }

  &-join {
    background: rgba(255,215,0,0.1);
    color: #ffd700;
    border: 1px solid #ffd700;

    &:hover {
      background: rgba(255,215,0,0.3);
    }
  }
}

.status-card {
  background: rgba(255,255,255,0.1);
  padding: 1.5rem;
  border-radius: 8px;
  text-align: center;

  .status-number {
    font-size: 2.5rem;
    font-weight: bold;
    color: #ffd700;
    margin: 0.5rem 0;
  }
}

.game-status {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 1rem;
  margin-bottom: 2rem;
}
</style>


<style lang="scss" scoped>
.home-container {
  max-width: 1200px;
  margin: 0 auto;
  padding: 20px;
  background: var(--game-bg-color, #1a1a1a);
  color: var(--text-primary, #fff);
}

.game-nav {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 2rem;
  padding: 1rem;
  background: rgba(0,0,0,0.3);
  border-radius: 8px;

  .user-profile {
    display: flex;
    align-items: center;
    gap: 1rem;

    .user-avatar {
      width: 40px;
      height: 40px;
      border-radius: 50%;
      border: 2px solid gold;
    }
  }
}

.room-list {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(280px, 1fr));
  gap: 1.5rem;
  margin: 2rem 0;
}

.room-card {
  background: var(--card-bg-color, #2d2d2d);
  padding: 1.5rem;
  border-radius: 12px;
  cursor: pointer;
  transition: transform 0.2s ease;

  &:hover {
    transform: translateY(-5px);
    box-shadow: 0 4px 15px rgba(255,215,0,0.2);
  }

  .room-header {
    display: flex;
    justify-content: space-between;
    margin-bottom: 1rem;

    .room-name {
      font-weight: bold;
      color: var(--accent-color, #ffd700);
    }

    .room-mode {
      padding: 0.2rem 0.5rem;
      border-radius: 4px;
      font-size: 0.8em;

      &.classic { background: #4CAF50 }
      &.ranked { background: #F44336 }
      &.private { background: #2196F3 }
    }
  }
}

.btn {
  padding: 0.8rem 1.5rem;
  border: none;
  border-radius: 6px;
  font-weight: bold;
  transition: all 0.3s ease;

  &-primary {
    background: linear-gradient(135deg, #ffd700, #ff9800);
    color: #1a1a1a;

    &:hover {
      opacity: 0.9;
    }
  }

  &-join {
    background: rgba(255,215,0,0.1);
    color: #ffd700;
    border: 1px solid #ffd700;

    &:hover {
      background: rgba(255,215,0,0.3);
    }
  }
}

.status-card {
  background: rgba(255,255,255,0.1);
  padding: 1.5rem;
  border-radius: 8px;
  text-align: center;

  .status-number {
    font-size: 2.5rem;
    font-weight: bold;
    color: #ffd700;
    margin: 0.5rem 0;
  }
}

.game-status {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 1rem;
  margin-bottom: 2rem;
}
</style>
